package hut.ims.client;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.widget.TextView;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.EditText;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.media.MediaRecorder.OnErrorListener;
import android.os.Bundle;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.util.*;

public class StreamingVideo extends Activity implements OnErrorListener, OnCompletionListener,SurfaceHolder.Callback{
/** Called when the activity is first created. */
private static final String TAG = "Video Player";
private ImageButton play_pause;
private ImageButton stop;
private ProgressBar progressbar;
private EditText editText;
private SurfaceView preview;
private SurfaceHolder holder;
private MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);
	play_pause = (ImageButton) findViewById(R.id.button_start_pause);
	stop = (ImageButton) findViewById(R.id.button_stop);
	progressbar = (ProgressBar) findViewById(R.id.progress_bar);
	editText = (EditText) findViewById(R.id.edittext);
	preview = (SurfaceView) findViewById(R.id.surface);
	
	play_pause.setOnClickListener(new View.OnClickListener(){

				@Override
				public void onClick(View view) {
					// TODO Auto-generated method stub
					playVideo();
				} } );
	}
public void playVideo(){
	try{
		final String path = editText.getText().toString();
		Log.v(TAG,"path : "+path);
		mp = new MediaPlayer();
		mp = MediaPlayer.create(getApplicationContext(),
				R.raw.abc);

		mp.setOnErrorListener((android.media.MediaPlayer.OnErrorListener) this);
		mp.setOnBufferingUpdateListener((OnBufferingUpdateListener) this);
		mp.setOnCompletionListener(this);
		mp.setOnPreparedListener((OnPreparedListener) this);
		mp.setAudioStreamType(2);
		mp.setDisplay((SurfaceHolder) preview.getHolder().getSurface());
		Runnable r = new Runnable(){
			public void run(){
				try {
					setDataSource(path);
				} catch (IOException e){Log.e(TAG, e.getMessage(),e);}
				try{
					mp = new MediaPlayer();
					mp = MediaPlayer.create(getApplicationContext(),
							R.raw.abc);

					mp.prepare();
				} catch (IOException e){Log.e(TAG, e.getMessage(),e);}
				Log.v(TAG,"Duration-->"+mp.getDuration());
				mp.start();
			}
		};
		new Thread(r).start();
	}catch (Exception e){ Log.e(TAG,"error: "+e.getMessage(),e); }

}

public void setDataSource(String path) throws IOException{
	URL url = new URL(path);
	URLConnection cn = url.openConnection();
	cn.connect();
	InputStream stream = cn.getInputStream();
	if (stream == null){
		TextView t = new TextView(this);
		t.setText("error");
		setContentView(t);
		throw new RuntimeException("stream is null");
	}
	TextView t = new TextView(this);
	t.setText("done");
	setContentView(t);

	File temp = File.createTempFile("videotemp", "dat");
	String tempPath = temp.getAbsolutePath();
	FileOutputStream out = new FileOutputStream(temp);
	byte buff[] = new byte[128];
	do {
		int numread = stream.read(buff);
		if (numread<=0) break;
		out.write(buff,0,numread);
	} while (true);
	mp.setDataSource(tempPath);
	try{
		stream.close();
	} catch (IOException ex) { Log.e(TAG, "error: " + ex.getMessage(), ex); }
	}

@Override
public void onError(MediaRecorder mr, int what, int extra) {
// TODO Auto-generated method stub

}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub

}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub

}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub

}

@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub

}
}
